package com.hdcircles.autoform.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.v7.app.AppCompatDelegate;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.cengalabs.flatui.FlatUI;
import com.hdcircles.autoform.component.CrashHandler;
import com.hdcircles.autoform.dagger.component.AppComponent;
import com.hdcircles.autoform.dagger.component.DaggerAppComponent;
import com.hdcircles.autoform.dagger.module.AppModule;

import java.util.HashSet;
import java.util.Set;

import im.fir.sdk.FIR;

/**
 * Created by hdcircles on 2016/9/21.
 */

public class App extends Application{
    private static App instance;

    private Set<Activity> allActivities;

    public static int SCREEN_WIDTH =-1;
    public static int SCREEN_HEIGHT =-1;
    public static float DIMEN_RATE=-1.0F;
    public static int DIMEN_DPI=-1;

    public static synchronized App getInstance(){
        return instance;
    }

    static {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;

        //初始化主题
        FlatUI.initDefaultValues(this);
        FlatUI.setDefaultTheme(FlatUI.SEA);

        //初始化屏幕宽高
        getScreenSize();

        //初始化日志
        com.orhanobut.logger.Logger.init(getPackageName()).hideThreadInfo();

        //初始化错误收集
        CrashHandler.init(new CrashHandler(getApplicationContext()));

//        //初始化内存泄露检测
//        LeakCanary.install(this);
//
//        //初始化过度绘制检测
//        BlockCanary.install(this,new AppBlockCanaryContext()).start();

        FIR.init(this);




    }


    public void addActivity(Activity activity){
        if (null==allActivities){
            allActivities=new HashSet<Activity>();
        }
        allActivities.add(activity);
    }

    public void removeActivity(Activity activity){
        if (null!=allActivities){
            allActivities.remove(activity);
        }
    }

    public void exitApp(){
        if (null!=allActivities){
            synchronized (allActivities){
                for (Activity activity:allActivities){
                    activity.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }



    public void getScreenSize(){
        WindowManager windowManager=(WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm=new DisplayMetrics();
        Display display=windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE=dm.density/1.0F;
        DIMEN_DPI=dm.densityDpi;
        SCREEN_WIDTH =dm.widthPixels;
        SCREEN_HEIGHT =dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT){
            int t= SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH=t;
        }
    }

    public static AppComponent getAppComponent(){
        return DaggerAppComponent.builder()
                .appModule(new AppModule(instance))
                .build();
    }


}
